﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jumpton.SFA.Server.BusinessRule;
using Jumpton.SFA.Server.Entity;
using Jumpton.Persistent;
using System.Data;
using Jumpton.Data;
using Jumpton.SFA.Server;
using System.Data.SqlClient;

namespace BusinessRule.Promotion
{
    public class PromotionsManager : BusinessLogic
    {
        #region 用户组

        private decimal _P_ID;
        public decimal P_ID
        {
            set { _P_ID = value; }
            get { return _P_ID; }
        }

        private decimal _Domain_id;
        public decimal Domain_id
        {
            set { _Domain_id = value; }
            get { return _Domain_id; }
        }
        #endregion

        #region 是否存在
        /// <summary>
        /// 是否存在
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool IsExists(string code, string name)
        {
            string sql = string.Format(@"SELECT COUNT(ID) FROM PROMOTIONS WHERE (CODE = '{0}') AND DOMAIN_ID = {2} AND ACTIVE = 1", code, name, Domain_id);
            object obj = SqlData.ExecuteScalar(sql);
            return (int)obj > 0;
        }
        /// <summary>
        /// 是否存在
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool IsExists(string code, string name, int i)
        {
            string sql = string.Format(@"SELECT COUNT(ID) FROM PROMOTIONS WHERE (CODE = '{0}') AND DOMAIN_ID = {2} AND ID <> {3} AND ACTIVE = 1", code, name, Domain_id, i);
            object obj = SqlData.ExecuteScalar(sql);
            return (int)obj > 0;
        }
        /// <summary>
        /// 活动产品是否存在
        /// </summary>
        /// <param name="prid">活动ID</param>
        /// <param name="psid">促销产品ID</param>
        /// <returns></returns>
        public bool IsExists(int prid, int psid)
        {
            string sql = string.Format(@"SELECT COUNT(ID) FROM PROMOTIONS_PRODUCTS WHERE PROMOTIONS_ID = {0} AND PRODUCTS_SATISFY_ID = {1} AND DOMAIN_ID = {2} AND ACTIVE = 1", prid, psid, Domain_id);
            object obj = SqlData.ExecuteScalar(sql);
            return (int)obj > 0;
        }
        /// <summary>
        /// 活动产品是否存在
        /// </summary>
        /// <param name="prid">活动ID</param>
        /// <param name="psid">促销产品ID</param>
        /// <param name="id">自增ID</param>
        /// <returns></returns>
        public bool IsExists(int prid, int psid, int id)
        {
            string sql = string.Format(@"SELECT COUNT(ID) FROM PROMOTIONS_PRODUCTS WHERE PROMOTIONS_ID = {0} AND PRODUCTS_SATISFY_ID = {1} AND DOMAIN_ID = {2} AND ID <> {3} AND ACTIVE = 1", prid, psid, Domain_id, id);
            object obj = SqlData.ExecuteScalar(sql);
            return (int)obj > 0;
        }
        #endregion

        public bool Save(PROMOTIONS pr)
        {
            if (pr != null)
            {
                pr.Save();
                return true;
            }
            else
                return false;
        }

        public bool Save(PROMOTIONS_PRODUCTS curr_entity)
        {
            if (curr_entity != null)
            {
                curr_entity.Save();
                return true;
            }
            else
                return false;
        }

        public PROMOTIONS Load(int ID)
        {
            return (PROMOTIONS)ObjectRow.Load(typeof(PROMOTIONS), string.Format("ID={0}", ID));
        }

        public PROMOTIONS_PRODUCTS Load_PP(int ID)
        {
            return (PROMOTIONS_PRODUCTS)ObjectRow.Load(typeof(PROMOTIONS_PRODUCTS), string.Format("ID={0}", ID));
        }

        public DataSet GetTableData(string ID)
        {
            string sql = string.Format(@"SELECT ID,CODE c,NAME n,DBO.GETDATESTRINGBYBIGINT([START_DATE]) s,DBO.GETDATESTRINGBYBIGINT(END_DATE) e,
            CASE WHEN DBO.FUNC_TO_DATE_TIME_INTEGER(GETDATE()) < [START_DATE] THEN '0' WHEN DBO.FUNC_TO_DATE_TIME_INTEGER(GETDATE()) > END_DATE THEN '2' ELSE '1' END st,[DESCRIPTION] de  
            FROM PROMOTIONS WHERE ID = {0} AND ACTIVE = 1 ORDER BY ID DESC", ID);

            DataSet ds = SqlData.OpenSql(sql);
            ds.Tables[0].TableName = "rows";

            return ds;
        }

        public DataSet GetTableData()
        {
            string sql = string.Format(@"SELECT ID,CODE c,NAME n,DBO.GETDATESTRINGBYBIGINT([START_DATE]) s,DBO.GETDATESTRINGBYBIGINT(END_DATE) e,
            CASE WHEN DBO.FUNC_TO_DATE_TIME_INTEGER(GETDATE()) < [START_DATE] THEN '0' WHEN DBO.FUNC_TO_DATE_TIME_INTEGER(GETDATE()) > END_DATE THEN '2' ELSE '1' END st,[DESCRIPTION] de  
            FROM PROMOTIONS WHERE [DOMAIN_ID] = {0} AND ACTIVE = 1 ORDER BY ID DESC", Domain_id);

            DataSet ds = SqlData.OpenSql(sql);
            ds.Tables[0].TableName = "rows";

            return ds;
        }

        public DataSet GetTableData(string pageSize, string currentPage, string promotionsId)
        {
            DataSet ds = new DataSet();

            string sql = string.Format(@"DECLARE	@return_value int,@COUNT int 
EXEC	@return_value = [dbo].[SP_GetPage]		@pageSize = {0},
		@currentPage = {1},
		@fields = N'PP.ID,PROMOTIONS_ID PRID,PRODUCTS_SATISFY_ID PSID,P1.CODE PSCODE,F2.CODE PSFCODE,F2.NAME PSFNAME,P1.[DESCRIPTION] PSNAME, SATISFY_VALUE SV, SATISFY_UOM SU, 
PRODUCTS_PRESENT_ID PPID,P2.CODE PPCODE,F2.CODE PPFCODE,F2.NAME PPFNAME,P2.[DESCRIPTION] PPNAME,PRESENT_VALUE PV, PRESENT_UOM PU, HANDSEL_TYPE HT ',
		@tablename = N'PROMOTIONS_PRODUCTS PP LEFT JOIN PRODUCTS P1 ON PP.PRODUCTS_SATISFY_ID = P1.ID LEFT JOIN FORMS F1 ON P1.FORM_CODE = F1.CODE 
LEFT JOIN PRODUCTS P2 ON PP.PRODUCTS_PRESENT_ID = P2.ID LEFT JOIN FORMS F2 ON P2.FORM_CODE = F2.CODE ',
		@orderString = N'PP.ID',
		@whereString = N' WHERE PP.ACTIVE = 1 AND PROMOTIONS_ID = {2}',
		@COUNT = @COUNT OUTPUT
SELECT	@COUNT as N'@COUNT'", pageSize, currentPage, promotionsId);

            ds = SqlData.OpenSql(sql);
            ds.Tables[0].TableName = "rows";

            return ds;
        }

        public static string getFormsData(decimal domainid)
        {
            string sql = string.Format(@"SELECT CODE ID,NAME FROM FORMS WHERE DOMAIN_ID = {0}", domainid);

            return JsonHelper.DataTableToJson(SqlData.OpenSql(sql).Tables[0]);
        }

        public string getProductsData(string formsId)
        {
            string sql = string.Format(@"SELECT ID,[DESCRIPTION] NAME FROM PRODUCTS WHERE FORM_CODE = '{0}'", formsId);

            return JsonHelper.DataTableToJson(SqlData.OpenSql(sql).Tables[0]);
        }

        /// <summary>
        /// 得到数据的Json字符串
        /// </summary>
        /// <returns></returns>
        public string GetTableJsonString(string pageSize, string currentPage, string promotionsId)
        {
            string sb = "";
            DataSet ds = GetTableData(pageSize, currentPage, promotionsId);
            if (ds != null && ds.Tables.Count > 0)
            {
                sb = JsonHelper.DataTableToJson(ds.Tables[0]);
                sb = sb.Insert(1, "\"total\":\"" + ds.Tables[1].Rows[0][0].ToString() + "\",");
            }

            return sb;
        }

        /// <summary>
        /// 得到数据的Json字符串
        /// </summary>
        /// <returns></returns>
        public string GetTableJsonString()
        {
            return GetJsonString(GetTableData());
        }

        /// <summary>
        /// 返回Json字符串
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        public string GetJsonString(DataSet ds)
        {
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                return JsonHelper.DataSetToJson(ds);
            }
            else
                return "";
        }
    }
}
